<?php

require_once 'Ext/User/Verify/Interface.php';

class Ext_User_Verify_Adapter_Db extends Ext_User_Verify_Abstract
{
    const IDENTITY_NOT_FOUND = 'identityNotFound';
    const CREDENTIAL_INVALID = 'credentialInvalid';
    protected $_messageTemplates = array(
        self::IDENTITY_NOT_FOUND => "A record with the supplied identity could not be found.",
        self::CREDENTIAL_INVALID => "Supplied credential is invalid.",
    );
    
    public function verify()
    {
        $result = array(
            'code'        => Ext_User_Verify_Result::FAILURE,
            'messages'    => array(),
        );

        $accountBase = App_Model_DbTable_User_Account_Base::getInstance()->getRowByUserId($this->_data->userId);
        if(!$accountBase)
        {
            $result['messages'][] = $this->_messager->createMessage(self::IDENTITY_NOT_FOUND);
            return new Ext_User_Verify_Result($result['code'], $result['messages']);
        }
        
        if($accountBase['password'] != md5($this->_data->password)){
            $result['messages'][] = $this->_messager->createMessage(self::CREDENTIAL_INVALID);
            return new Ext_User_Verify_Result($result['code'], $result['messages']);
        }
        
        $result['code'] = Ext_User_Verify_Result::SUCCESS;
        return new Ext_User_Verify_Result($result['code'], $result['messages']);
    }
}
